package com.qinhailin.portal.demo.ctr;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Model;
import com.qinhailin.common.model.DBook;
import com.qinhailin.common.routes.ControllerBind;
import com.qinhailin.portal.demo.srvice.DBookService;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author:jzz
 * @date:2020/8/10
 */
@ControllerBind(path = "/portal/demo/book")
public class DBookController extends Controller {

    @Inject
    DBookService dBookService;

    public void index () {
        render("book.html");
    }

    public void getList () {
        Model<DBook> dao = dBookService.getDao();
        List<DBook> all = dao.findAll();
        set("bookList",all);
        render("book.html");
    }

    public void saveA () {
        DBook book = getBean(DBook.class);
        book.setName(get("name"));
        book.setType(get("type"));
        book.setAuthor(get("author"));
        book.setPrice(new BigDecimal(Double.parseDouble(get("price"))));
        boolean save = book.save();
        if(save) {
            set("saveMsg","保存成功");
        }else {
            set("saveMsg","保存失败");
        }
        render("book.html");
    }

    public void updateA () {
        DBook book = getBean(DBook.class);
        book.setId(Integer.parseInt(get("id")));
        book.setName(get("name"));
        book.setType(get("type"));
        book.setAuthor(get("author"));
        book.setPrice(new BigDecimal(Double.parseDouble(get("price"))));
        boolean update = book.update();
        if(update) {
            set("updateMsg","修改成功");
        }else {
            set("updateMsg","修改失败");
        }
        render("book.html");
    }

    public void deleteById () {
        DBook book = getBean(DBook.class);
        boolean id = book.deleteById(Integer.parseInt(get("id")));
        if(id) {
            set("deleteMsg","删除成功");
        }else {
            set("deleteMsg","删除失败");
        }
        render("book.html");

    }






}
